#include "FSMTest.h"
#include "FSMMgr.h"

#include <iostream>
#include <sstream>

using namespace std;

CTransitionTest::CTransitionTest(SmartPtr<CStateBase> &pState, SmartPtr<CStateBase> &pNextState) : CTransition(pState, pNextState)
{
}

bool_t CTransitionTest::IsTrigger()
{
	cout << "Trigger:" << endl;

	return true;
}

CStateTest::CStateTest(const char_t *szName) : CState(szName)
{
}

CActionTest::CActionTest(CString szMessage) :  m_szMessage(szMessage)
{
}

void CActionTest::Execute()
{
	cout << m_szMessage << endl;
}

CFSMTest::CFSMTest()
{
}


void AddActions(int_t num, CStateTestPtr pState)
{
	for(int i = 0; i < num; i++)
	{
		stringstream msgEntry;
		msgEntry << "Entry action" << i << pState->GetName();
		pState->AddEntryAction(new CActionTest(msgEntry.str()));
		stringstream msgExit;
		msgExit << "Exit action" << i << pState->GetName();
		pState->AddEntryAction(new CActionTest(msgExit.str()));
		stringstream msgProcess;
		msgProcess << "Process action" << i << pState->GetName();
		pState->AddEntryAction(new CActionTest(msgProcess.str()));
	}
}


CStateTestPtr AddState(const char_t *szName, CStateGraphPtr pStateGraph, bool_t isInit)
{
	CStateTestPtr pState = new CStateTest(szName);
	AddActions(4, pState);

	if(isInit)
	{
		pStateGraph->SetInitialState(pState);
	}
	else
	{
		pStateGraph->AddState(pState);
	}
	return pState;

	return NULL;
}

void AddTransition(CStateTestPtr pState0, CStateTestPtr pState1)
{

}

void CFSMTest::Execute()
{
	CStateGraph *pRoot = new CStateGraph("StateGraph");
	CStateTestPtr pState0 = AddState("State0", pRoot, true);
	CStateTestPtr pState1 = AddState("State1", pRoot, false);
	CStateTestPtr pState2 = AddState("State2", pRoot, false);
	CStateTestPtr pState3 = AddState("State3", pRoot, false);
	CStateTestPtr pState4 = AddState("State4", pRoot, false);

	pState0->AddTransition( new CTransition(pState0, pState1));
	pState1->AddTransition( new CTransition(pState1, pState2));
	pState2->AddTransition( new CTransition(pState2, pState3));
	pState3->AddTransition( new CTransition(pState3, pState4));
	//pState4->AddTransition( new CTransition(pState4, pState0));

	CFSMMgr FSMMgr;
	FSMMgr.SetStateGraph(pRoot);

	while(true)
	{
		FSMMgr.Update();
	}
}

